<?php
	require_once ('../businessObjects/userAccount.php');
	
	session_start();
	
	$action = $_GET['ACTION'];	
	$message = '';
	if ($action == 'LOGIN'){

		if ($_POST['hddVal'] == 1){
			$user = new userAccount();
			$username = $_POST['txtUser'];
			$userpass = $_POST['txtPassword'];
			if ($user->userAccountLogin($username,$userpass)){
				$user->userAccountInformation($username);
				$result = mysqli_fetch_array($user->resultSet(),MYSQLI_ASSOC);		
				$_SESSION['username'] = $username;
				$_SESSION['enterpriseID'] = $result['enterpriseID'];
				header('Location: main.php');		
			}else{$message = $user->message();}
		}		
	}
	if ($action == 'LOGOUT'){	
		$username = $_SESSION['username'];
		if ($username != ""){
					$user = new userAccount();
					$user->userAccountLogOut ($username);				
				$_SESSION['username'] = '';
			}		
	}
	if ($action == 'SESSION_TIMEOUT'){
		$message = 'Session time out/ Session expired';
	}
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>RTMS by EinsCompany</title>
    <style type="text/css">@import "css/layout.css";@import "css/styles.css";  </style>    
    <script src="js/jquery-1.10.2.js" type="text/javascript" language="javascript"> </script>
    <script src="js/jquery.validate.min.js" type="text/javascript" language="javascript"> </script>
    <script type="application/javascript" language="javascript">
	$(document).ready(function(){
	   	$('#form1').validate({
			rules:{
				txtUser: {required:true},
				txtPassword: {required:true}
				},
			messages:{
				txtUser: {required:"*"},
				txtPassword: {required:"*"}
			}
		});
	});    
    </script>
</head>
<body>
<div id="container">
        <div id="header"><h1></h1><div id="section0"></div></div>
        <div id="nav_menu"></div>
        <div id="workspace"> 
        <form id="form1" action="?ACTION=LOGIN" method="post">	
    	<div id="cntdr_frm_login">
        <div class="cntdr_frm_login_tittle">Retail Management System</div>
        <div class="cntdr_frm_login_field">User:</div>
        <div class="cntdr_frm_login_data"><input name="txtUser" type="text" id="txtUser" class="controlTextBox" /></div>
        <div class="cntdr_frm_login_field">Password:</div>
        <div class="cntdr_frm_login_data"><input name="txtPassword" type="password" id="txtPassword" class="controlTextBox" /></div>
        <div class="cntdr_frm_login_conf"><input type="submit" name="btnIngresar" value="Log in" id="btnIngresar" class="controlSubmit" /></div>        
        <div class="cntdr_frm_login_msgs"><span id="lblMensajes"><?php echo $message; ?></span></div>
        <input type="hidden" id="hddVal" name="hddVal" value="1">
    	</div>
		</form>       	
		</div>
    </div>
</body>
</html>